<?php
/**
 * 公告管理
 * @author juxuan
 *
 */

// 公告管理
class BulletinAdminAction extends Action{
	
	public function __construct(){
		parent::__construct();
		if(!isset($_SESSION['admin_id'])){
			header("Content-Type:text/html; charset=utf-8");
			$this->redirect('Index/login',null,3,"非法登录,3秒后为你跳转到登录页面..");
		}
	}
    
	//公告管理首页：新闻列表；分页；每页显示条数：15
	public function index(){
		
    	import("ORG.Util.Page");
    	
    	$Bulletin = new Model('Bulletin');
    	$count = $Bulletin->count();
    	$Page = new Page($count, 15);
    	$show = $Page->show();
    	
    	$list = $Bulletin->order('bulletin_id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
    	$this->assign('bulletinList',$list);
    	$this->assign('page',$show);    	
    	$this->display();
    	
    }
    
    
    public function add(){
    	$this->display();
    }
    

    /**
     +----------------------------------------------------------
     * 添加公告的表单处理
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @author juxuan
     +----------------------------------------------------------
     * @return 
     +----------------------------------------------------------
     */
    
    //添加公告的表单处理
    public function insertBulletin(){
    	
      	//如果上传附件，则将附件上传 	
    	if(isset($_POST['isFile'])){	
			import("ORG.Net.UploadFile");	//导入文件上传类	
	    	$upload = new UploadFile();		//实例化上传类
	    	$upload->savePath = 'Public/bulletinAffix/';	//设置附件上传目录
		 //   $upload->saveRule = time;	//设置上传文件规则
		    
		    if(!$upload->upload()){	//上传错误，提示错误信息
		    	header("Content-Type:text/html; charset=utf-8");
    			exit($upload->getErrorMsg().'[<a href="javascript:history.back()">返回</a>]');
		    	//$this->error($upload->getErrorMsg().'hhh');
		    }
		    else{
		    	$uploadList = $upload->getUploadFileInfo();	//上传成功，获取上传文件信息
		    	
		    }
		    
		    
    		$BulletinForm = D("Admin.Bulletin");	//插入公告的主体内容
        	if($vo = $BulletinForm->create()){
	    		if($bulletinID = $BulletinForm->add()){
	    	
    				$BulletinAffix = M('Bulletinaffix');	//在公告附件表插入附件信息
				    foreach ($uploadList as $vo){
				    	$affix['bulletin_id'] = $bulletinID;
				    	$affix['filename'] = $vo['savename'];
				    	$affix['showname'] = $vo['savename'];
				    	$BulletinAffix->add($affix);
					    
	    			}
				   
	    			header("Content-Type:text/html; charset=utf-8");
    				exit('公告发布成功！[有附件]'.'[<a href="javascript:history.back()">返回</a>]');
	    		
	    		}else{
	    			header("Content-Type:text/html; charset=utf-8");
    				exit('公告发布失败！[有附件]'.'[<a href="javascript:history.back()">返回</a>]');
	    	
	    		}
	    	}else{
	    		header("Content-Type:text/html; charset=utf-8");
    			exit($BulletinForm->getError().'[<a href="javascript:history.back()">返回</a>]');
	    	}    
    	}
    	else{	
    			//不上传附件的情况
    			$BulletinForm = D("Admin.Bulletin");
    	    	if($vo = $BulletinForm->create()){
		    		if($bulletinID = $BulletinForm->add()){
		    			header("Content-Type:text/html; charset=utf-8");
		    			exit('公告发布成功[无附件]！'.'[<a href="javascript:history.back()">返回</a>]');	
		    		}else{
						header("Content-Type:text/html; charset=utf-8");
						exit('公告发布失败[无附件]！'.'[<a href="javascript:history.back()">返回</a>]');
		    			
		    		}
		    	}else{
		    		header("Content-Type:text/html; charset=utf-8");
		    		exit($BulletinForm->getError().'[<a href="javascript:history.back()">返回</a>]');
		    	}
    	}    		
    	
    }
    
	/**
     +----------------------------------------------------------
     * 更新公告的表单处理
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @author juxuan
     +----------------------------------------------------------
     * @return 
     +----------------------------------------------------------
     */
    public function updateBulletin(){	//更新公告，通过_GET方法读取bulletin_id，以此从数据库获取该公告的信息；进入公告更新页面
    	   	
    	$Bulletin = new Model('Bulletin');
    	$bulletin_id = (int)$_GET['bulletin_id']; 	
		$bulletin = $Bulletin->where('bulletin_id='.$bulletin_id)->find();
		
		$BulletinAffix = new Model('Bulletinaffix');	//公告的附件
		$bulletinAffixList = $BulletinAffix->where('bulletin_id='.$bulletin_id)->findAll();
		
		$this->assign('bulletin',$bulletin);
		$this->assign('bulletinAffixList',$bulletinAffixList);
		$this->display();
    	
    }
    
    
	public function updateCommit(){	//更新确认提交
    	
    	
    	if(isset($_POST['isFile'])){	//更新操作若有添加新的附件，则将附件上传	
			import("ORG.Net.UploadFile");	//导入文件上传类	
	    	$upload = new UploadFile();		//实例化上传类
	    	$upload->savePath = 'Public/bulletinAffix/';	//设置附件上传目录
		 //   $upload->saveRule = time;	//设置上传文件规则
		    
		    if(!$upload->upload()){	//上传错误，提示错误信息
		    	header("Content-Type:text/html; charset=utf-8");
    			exit($upload->getErrorMsg().'公告更新附件上传错误！[<a href="javascript:history.back()">返回</a>]');
		    }
		    else{
		    	$uploadList = $upload->getUploadFileInfo();	//上传成功，获取上传文件信息
		    }
		    
    		$BulletinForm = D("Bulletin");
	    	
	    	$bulletin_id = (int)$_POST['bulletin_id'];
	    	
	    	$BulletinForm->find($bulletin_id);
	    	$BulletinForm->title = $_POST['title'];
	    	$BulletinForm->subtitle = $_POST['subtitle'];
	    	$BulletinForm->type = $_POST['type'];
	    	$BulletinForm->message = $_POST['message'];
	    	
	    	if(isset($_POST['subtype'])){
	    		$BulletinForm->subtype = $_POST['subtype'];
	    	}
	    	else{
	    		$BulletinForm->subtype = null;
	    	}
	    	
    		$BulletinAffix = M('Bulletinaffix');	//在公告附件表插入附件信息
			foreach ($uploadList as $vo){
		    	
				$affix['bulletin_id'] = $bulletin_id;
		    	$affix['filename'] = $vo['savename'];
		    	$affix['showname'] = $vo['savename'];
		    	$BulletinAffix->add($affix);
				    
    		}
	    	
	    	
	    	if($BulletinForm->save()){
	    		
	    		header("Content-Type:text/html; charset=utf-8");
    			exit('公告更新成功！[有附件]'.'[<a href="javascript:history.back()">返回</a>]');
	    	}else{
	    		header("Content-Type:text/html; charset=utf-8");
    			exit('公告更新失败！[有附件]'.'[<a href="javascript:history.back()">返回</a>]');
	    	}
	    	  	
    	}else{	//更新操作没有添加新的附件的情况
	    	$BulletinForm = D("Bulletin");
	    	
	    	$bulletin_id = (int)$_POST['bulletin_id'];
	    	
	    	$BulletinForm->find($bulletin_id);
	    	$BulletinForm->title = $_POST['title'];
	    	$BulletinForm->subtitle = $_POST['subtitle'];
	    	$BulletinForm->type = $_POST['type'];
	    	$BulletinForm->message = $_POST['message'];
	    	
    		if(isset($_POST['subtype'])){
	    		$BulletinForm->subtype = $_POST['subtype'];
	    	}
	    	else{
	    		$BulletinForm->subtype = null;
	    	}
    	
	    	if($BulletinForm->save()){
	    		header("Content-Type:text/html; charset=utf-8");
    			exit('公告更新成功！[无附件]'.'[<a href="javascript:history.back()">返回</a>]');	
	
	    	}else{
	    		header("Content-Type:text/html; charset=utf-8");
    			exit('公告更新失败！[无附件]'.'[<a href="javascript:history.back()">返回</a>]');
	    	}
	    	
    	}  	  	
    }
    

    /**
     +----------------------------------------------------------
     * 删除公告的处理
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @author juxuan
     +----------------------------------------------------------
     * @return 
     +----------------------------------------------------------
     */
    

	//跳转到公告删除确认询问页面
    public function deleteBulletin(){
    	$BulletinForm = D("Bulletin");
    	$bulletin_id = (int)$_GET['bulletin_id'];
    	
    	$this->assign("bulletin_id",$bulletin_id);
    	$this->display(); 	
    }
  	
    
    //删除确认提交
    public function deleteCommit(){
    	$BulletinForm = D("Bulletin");
    	$bulletin_id = (int)$_POST['bulletin_id'];
    	
    	$BulletinForm->find($bulletin_id);
	
    	if($BulletinForm->delete()){
    		//删除公告的附件
    		$Bulletinaffix = D("Bulletinaffix");
    		$Bulletinaffix->where('bulletin_id='.$bulletin_id)->delete();
    		
    		$this->ajaxReturn(0,'公告删除成功！',1);	

    	}else{
    		$this->ajaxReturn('公告删除失败！');
    	}
    	
    	
    }
    

    /**
     +----------------------------------------------------------
     * 删除公告的附件
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @author juxuan
     +----------------------------------------------------------
     * @return 
     +----------------------------------------------------------
     */   
    
    public function deleteAffix(){
    	
    	$Bulletinaffix = D("Bulletinaffix");
    	$affix_id = (int)$_GET['affix_id'];
    	$bulletin_id = (int)$_GET['bulletin_id'];
    	
    	$Bulletinaffix->find($affix_id);
		
    	if($Bulletinaffix->delete()){
    		
    		$this->redirect('BulletinAdmin/updateBulletin/bulletin_id/'.$bulletin_id);
    	}
 	
    }
    
}
?>